<template>
	<view class="invoice-page">
		<view class="header">
			<view class="title">选择开票订单</view>
			<view class="subtitle">请选择需要开具发票的订单</view>
		</view>
		
		<view class="order-list">
			<view class="order-item" v-for="(item, index) in orderList" :key="item.orderId" @click="selectOrder(index)">
				<view class="order-info">
					<view class="time">{{formatTime(item.orderTime)}}</view>
					<view class="order-number">订单编号：{{item.orderNumber}}</view>
					<view class="route">
						<view class="car-no">{{item.carNo}}</view>
					</view>
					<view class="price">￥{{item.transAmount.toFixed(2)}}</view>
				</view>
				<view class="radio" :class="{'selected': selectedIndex === index}">
					<view class="inner" v-if="selectedIndex === index"></view>
				</view>
			</view>
		</view>
		
		<view class="bottom-bar">
			<view class="total">
				<text>合计：</text>
				<text class="amount">￥{{totalAmount}}</text>
			</view>
			<view 
				class="confirm-btn" 
				:class="{'disabled': selectedIndex === -1}"
				@click="selectedIndex !== -1 && confirmInvoice()"
			>确认开票</view>
		</view>
	</view>
</template>

<script>
	import { getInvoiceableOrders } from '@/api/invoice'
	
	export default {
		data() {
			return {
				orderList: [], // 订单列表
				selectedIndex: -1, // 选中的订单索引
				pageNum: 1,
				pageSize: 10
			}
		},
		computed: {
			totalAmount() {
				if (this.selectedIndex === -1) return '0.00'
				return this.orderList[this.selectedIndex]?.transAmount.toFixed(2) || '0.00'
			}
		},
		methods: {
			// 格式化时间
			formatTime(timeStr) {
				const date = new Date(timeStr)
				const year = date.getFullYear()
				const month = String(date.getMonth() + 1).padStart(2, '0')
				const day = String(date.getDate()).padStart(2, '0')
				const hours = String(date.getHours()).padStart(2, '0')
				const minutes = String(date.getMinutes()).padStart(2, '0')
				return `${year}-${month}-${day} ${hours}:${minutes}`
			},
			// 选择订单
			selectOrder(index) {
				this.selectedIndex = this.selectedIndex === index ? -1 : index
			},
			// 确认开票
			confirmInvoice() {
				const selectedOrder = this.orderList[this.selectedIndex]
				uni.navigateTo({
					url: `/pages/invoice/invoice_form?orderInfo=${encodeURIComponent(JSON.stringify(selectedOrder))}`
				})
			},
			// 获取订单列表
			async getOrderList() {
				try {
					const res = await getInvoiceableOrders(this.pageNum, this.pageSize)
					
					this.orderList = res
					
				} catch (error) {
					uni.showToast({
						title: '获取订单列表失败',
						icon: 'none'
					})
				}
			}
		},
		onLoad() {
			this.getOrderList()
		}
	}
</script>

<style lang="scss" scoped>
	.invoice-page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
		
		.header {
			background-color: #fff;
			padding: 40rpx 30rpx;
			
			.title {
				font-size: 36rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
			}
			
			.subtitle {
				font-size: 28rpx;
				color: #666;
			}
		}
		
		.order-list {
			padding: 20rpx 30rpx;
			
			.order-item {
				background-color: #fff;
				border-radius: 16rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.order-info {
					flex: 1;
					
					.time {
						font-size: 26rpx;
						color: #999;
						margin-bottom: 20rpx;
					}
					
					.order-number {
						font-size: 26rpx;
						color: #666;
						margin-bottom: 16rpx;
					}
					
					.route {
						display: flex;
						align-items: center;
						margin-bottom: 20rpx;
						font-size: 30rpx;
						
						.arrow {
							margin: 0 20rpx;
							color: #999;
						}
					}
					
					.price {
						font-size: 32rpx;
						font-weight: bold;
						color: #ff6b00;
					}
					
					.car-no {
						font-size: 30rpx;
						color: #333;
						margin-bottom: 10rpx;
					}
				}
				
				.radio {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					border: 2rpx solid #ddd;
					margin-left: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					
					&.selected {
						border-color: #3175ff;
						
						.inner {
							width: 24rpx;
							height: 24rpx;
							border-radius: 50%;
							background-color: #3175ff;
						}
					}
				}
			}
		}
		
		.bottom-bar {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 100rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;
			box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
			
			.total {
				font-size: 28rpx;
				
				.amount {
					color: #ff6b00;
					font-weight: bold;
					font-size: 32rpx;
				}
			}
			
			.confirm-btn {
				background-color: #3175ff;
				color: #fff;
				padding: 20rpx 60rpx;
				border-radius: 40rpx;
				font-size: 28rpx;
				
				&.disabled {
					background-color: #cccccc;
					color: #ffffff;
					opacity: 0.8;
				}
			}
		}
	}
</style> 